﻿using System;
using System.Drawing;
using System.Windows.Forms;
using LevelEditor.Document;
using LevelEditor.View;
using WeifenLuo.WinFormsUI.Docking;

namespace LevelEditor.Forms
{
    public partial class PaletteForm : DockContent
    {
        #region Fields

        int LayerIndex;

        bool gridVisible = true;

        decimal gridThickness = 1;

        #endregion

        #region Constructors

        public PaletteForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Event Handlers

        public delegate void OptionsEventHandler(object sender, EventArgs e);
        public event OptionsEventHandler Options;
        protected void OnOptions(EventArgs e)
        {
            if (Options != null)
                Options(this, e);
        }

        public delegate void TileDefinitionChangeHandler(object sender, EventArgs e);
        public event TileDefinitionChangeHandler TileDefinitionChange;
        protected void OnTileDefinitionChanged(EventArgs e)
        {
            if (TileDefinitionChange != null)
                TileDefinitionChange(this, e);
        }

        #endregion

        #region Methods

        public void RebuildPages(MainForm form)
        {
            Layer currentLayer = form.Level.Layers[LayerIndex];
            pc.Reset(currentLayer.TileSet);
        }

        public void ResetToCurrentLayer(MainForm form)
        {
            if (form.CurrentLayer != LayerIndex)
                LayerIndex = form.CurrentLayer;
            RebuildPages(form);
        }

        public void ResetBitmap(MainForm form)
        {
            ResetToCurrentLayer(form);
        }

        public void SetPaletteGridColor(Color gridColor)
        {
            ForeColor = gridColor;
            pc.ForeColor = gridColor;
            Invalidate();
        }

        public void SetPaletteBackColor(Color backColor)
        {
            BackColor = backColor;
            pc.BackColor = backColor;
            Invalidate();
        }

        public bool GetGridVisible()
        {
            return gridVisible;
        }

        public void SetGridVisible(bool visible)
        {
            gridVisible = visible;
            pc.GridVisible = gridVisible;
        }

        public decimal GetGridThickness()
        {
            return gridThickness;
        }

        public void SetGridThickness(decimal thickness)
        {
            gridThickness = thickness;
            pc.GridThickness = gridThickness;
        }

        #endregion

        #region Events

        private void PaletteForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OnOptions(e);
        }


        private void tileDefinitionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OnTileDefinitionChanged(e);
        }

        #endregion

    }
}
